package com.back.controller;

import com.back.annotation.AuthCheck;
import com.back.common.BaseResponse;
import com.back.common.DeleteRequest;
import com.back.common.ResultUtils;
import com.back.constant.UserConstant;
import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.exception.ThrowUtils;
import com.back.model.dto.hero.HeroAddRequest;
import com.back.model.dto.hero.HeroQueryRequest;
import com.back.model.dto.hero.HeroUpdateRequest;
import com.back.model.vo.hero.HeroVO;
import com.back.service.HeroService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 英雄接口
 */
@RestController
@RequestMapping("/hero")
@Slf4j
public class HeroController {

    @Resource
    private HeroService heroService;

    // region 增删改查

    /**
     * 创建英雄
     *
     * @param heroAddRequest 英雄添加请求
     * @return 英雄ID
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addHero(@RequestBody  HeroAddRequest heroAddRequest) {
        ThrowUtils.throwIf(heroAddRequest == null, ErrorCode.PARAMS_ERROR);
        
        long heroId = heroService.addHero(heroAddRequest);
        return ResultUtils.success(heroId);
    }

    /**
     * 删除英雄
     *
     * @param deleteRequest 删除请求
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteHero(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        long id = deleteRequest.getId();
        boolean result = heroService.deleteHero(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新英雄（仅管理员）
     *
     * @param heroUpdateRequest 英雄更新请求
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateHero(@RequestBody  HeroUpdateRequest heroUpdateRequest) {
        if (heroUpdateRequest == null || heroUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        boolean result = heroService.updateHero(heroUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取英雄（封装类）
     *
     * @param id 英雄ID
     * @return 英雄VO
     */
    @GetMapping("/get/vo")
    public BaseResponse<HeroVO> getHeroVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        HeroVO heroVO = heroService.getHeroVOById(id);
        return ResultUtils.success(heroVO);
    }

    /**
     * 分页获取英雄列表（仅管理员）
     *
     * @param heroQueryRequest 查询请求
     * @return 英雄VO分页列表
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<IPage<HeroVO>> listHeroVOByPage(@RequestBody HeroQueryRequest heroQueryRequest) {
        long current = heroQueryRequest.getCurrent();
        long size = heroQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        
        IPage<HeroVO> heroVOIPage = heroService.listHeroVOByPage(heroQueryRequest);
        return ResultUtils.success(heroVOIPage);
    }

    /**
     * 获取所有英雄列表（用于前端展示）
     *
     * @return 英雄VO列表
     */
    @GetMapping("/list/all")
    public BaseResponse<List<HeroVO>> getAllHeroVOList() {
        List<HeroVO> heroVOList = heroService.getAllHeroVOList();
        return ResultUtils.success(heroVOList);
    }

    // endregion

}